package org.sems.sems.service.impl;

import org.sems.sems.Mapper.XsxxkMapper;
import org.sems.sems.Mapper.XxdmkMapper;
import org.sems.sems.entity.Xxdmk;
import org.sems.sems.service.XxdmkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XxdmkServiceImpl implements XxdmkService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private XxdmkMapper xxdmkMapper;
    /**
     * 获取消息信息
     *
     * @param yhm    用户名
     * @param yhsfdm 用户身份代码
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> getXxdmk(String yhm, int yhsfdm) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // 处理获取消息信息
            List<Map<String, Object>> resultList = new ArrayList<>();
            resultList = jdbcTemplate.queryForList(
                    "{call usp_getXxdmkData(?,?)}", yhsfdm, yhm);
            resultMap.put("data", resultList);
            resultMap.put("code", 200);
            resultMap.put("msg", "success");
            resultMap.put("result", true);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "server error");
            resultMap.put("result", false);
            resultMap.put("data", e.getMessage());
        }
        return resultMap;
    }

    /**
     * 设置已读
     *
     * @param yhm    用户名
     * @param yhsfdm 用户身份代码
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> setRead(String yhm, int yhsfdm) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            //  添加工作经历数据
            if (xxdmkMapper.setAllRead(yhm, yhsfdm) > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "success");
                resultMap.put("result", true);
                resultMap.put("data", "已读成功");
            } else {
                resultMap.put("code", 500);
                resultMap.put("msg", "database error");
                resultMap.put("result", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "server error");
            resultMap.put("result", false);
            resultMap.put("data", e.getMessage());
        }
        return resultMap;
    }

    /**
     * 添加消息
     *
     * @param xxdmk 消息对象
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> addXxdmk(Xxdmk xxdmk) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            //  添加工作经历数据
            if (xxdmkMapper.addNewXxdmk(xxdmk) > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "success");
                resultMap.put("result", true);
                resultMap.put("data", xxdmk);
            } else {
                resultMap.put("code", 500);
                resultMap.put("msg", "database error");
                resultMap.put("result", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "server error");
            resultMap.put("result", false);
            resultMap.put("data", e.getMessage());
        }
        return resultMap;
    }
}
